<?php
/**
 * Created by PhpStorm.
 * Loan: ASUS
 * Date: 2020/8/24
 * Time: 10:59
 */

namespace app\pcapi\controller;

//系统管理的业务逻辑
class Admin extends Base
{
    /**
     * @author ASUS
     * @date 2020-08-25 10:29
     * 管理员列表
     */
    public function adminIndex()
    {
        $cache = cache(input("admin"));
        if($cache["id"]==1) {//超管
            $where[] = ["id",">","1"];
        }else{//各自盘
            $where[] = ["id","=",$cache["id"]];
        }
        $page = input('page',1);//分页，开始页数
        $limit = input('limit',10);//每页的条数
        $start = ($page-1) * $limit;//分页起始位置
        $list = db("admin")->where($where)->limit($start,$limit)->select();
        $count = db("admin")->where($where)->count();
        $data = [
            'list'=>$list,
            'count'=>$count
        ];
        api_return(200,"成功",$data);
    }
    /**
     * @author ASUS
     * @date 2020-08-25 10:29
     * 添加管理员
     */
    public function addAdmin()
    {
        $cache = cache(input("admin"));
        if($cache["id"]!=1) {//不是超管，无此权限
            api_return(203,'无权限',[]);
        }
        $admin = input("admin_name");
        $password = input("password");
        $data = [
            'admin'=>$admin,
            'password'=>$password,
            'e_mail'=>input('e_mail',"#"),
            'add_time'=>time(),
        ];
        $validate = new \app\pcapi\validate\Admin();
        if (!$validate->check($data)) {
            api_return(203,$validate->getError(),[]);
        }
        $data['password'] = md5($password);
        $res = db("admin")->insertGetId($data);
        if(!$res){
            api_return(203,'失败',[]);
        }
        api_return(200,'成功',["id"=>$res]);
    }
    /**
     * @author ASUS
     * @date 2020-08-25 10:30
     * 根据id获取
     * 管理员信息
     */
    public function getAdminInfo()
    {
        $id = input("id");
        if(empty($id)){
            api_return(203,'参数缺失',[]);
        }
        $info = db("admin")->where(['id'=>$id])->find();
        $product = db("admin_product_amount")
            ->alias("a")
            ->leftJoin("product b","a.product_id=b.id")
            ->where(["admin_id"=>$id])
            ->field("a.*,b.product_name")
            ->select();
        $data = [
            "info"=>$info,
            "product"=>$product
        ];
        api_return(200,'成功',$data);
    }

    /**
     * @author ASUS
     * @date 2020-08-25 10:30
     * 根据id修改
     * 管理员信息
     */
    public function updateAdmin()
    {
        $id = input("id");
        if(empty($id)){
            api_return(203,'参数缺失',[]);
        }
        $data = [
            'admin'=>input('admin_name'),
            'password'=>md5(input("password")),
            'e_mail'=>input('e_mail',""),
        ];
        $info = db("admin")->where(['id'=>$id])->find();
        if($info["password"]==input("password")){
            unset($data["password"]);
        }
        $res = db("admin")->where(['id'=>$id])->update($data);
        if(!$res){
            api_return(203,'失败',[]);
        }
        api_return(200,'成功',[]);
    }

    /**
     * @author ASUS
     * @date 2020-08-25 10:30
     * 根据id修改
     * 管理员登陆状态
     * 1正常0禁用
     */
    public function updateAdminStatus()
    {
        $cache = cache(input("admin"));
        if($cache["id"]!=1) {//不是超管，无此权限
            api_return(203,'无权限',[]);
        }
        $id = input("id");
        if(empty($id)){
            api_return(203,'参数缺失',[]);
        }
        $status = input("status");
        $res = db("admin")->where(['id'=>$id])->setField("status",$status);
        if(!$res){
            api_return(203,'失败',[]);
        }
        api_return(200,'成功',[]);
    }
    /**
     * @author ASUS
     * @date 2020-08-25 10:31
     * 根据id删除管理员
     */
    public function deleteAdmin()
    {
        $cache = cache(input("admin"));
        if($cache["id"]!=1) {//不是超管，无此权限
            api_return(203,'无权限',[]);
        }
        $id = input("id");
        if(empty($id)){
            api_return(203,'参数缺失',[]);
        }
        $info = db("admin")->where(["id"=>$id])->find();
        cache($info["admin"],null);
        db("admin")->where(['id'=>$id])->delete();
        db("admin_product_amount")->where(['admin_id'=>$id])->delete();
        api_return(200,'成功',$info);
    }

    /**
     * @author ASUS
     * @date 2021-09-07 14:14
     * 开通产品
     */
    public function addProduct()
    {
        $admin_id = input("admin_id");
        $product_id = input("product_id");
        $d_free = input("d_free");
        $z_free = input("z_free");
        $pro_data = [
            "admin_id"=>$admin_id,
            "product_id"=>$product_id,
            "d_free"=>$d_free,
            "z_free"=>$z_free,
        ];
        $res = db("admin_product_amount")->insertGetId($pro_data);
        if(!$res){
            api_return(203,'失败',[]);
        }
        api_return(200,'成功',[]);
    }

    /**
     * @author ASUS
     * @date 2021-09-07 14:27
     * 充值
     */
    public function charge()
    {
        $admin_id = input("admin_id");
        $product_id = input("product_id");
        $amount = input("amount");
        $pro_data = [
            "admin_id"=>$admin_id,
            "product_id"=>$product_id,
            "amount"=>$amount,
            "add_time"=>time(),
        ];
        db("admin_charge")->insert($pro_data);
        db("admin_product_amount")->where(["admin_id"=>$admin_id,"product_id"=>$product_id])->setInc("amount",$amount);
        api_return(200,'成功',[]);
    }

    /**
     * @author ASUS
     * @date 2021-09-16 11:41
     * 账户明细
     */
    public function amountDetail()
    {
        $admin_id = input("admin_id");
        if(empty($admin_id)){
            api_return(203,'参数缺失',[]);
        }
        $page = input('page',1);//分页，开始页数
        $limit = input('limit',10);//每页的条数
        $start = ($page-1) * $limit;//分页起始位置
        $list = db("admin_amount_detail")
            ->alias("a")
            ->leftJoin("product b","a.product_id=b.id")
            ->field("a.*,b.product_name")
            ->where(["admin_id"=>$admin_id])
            ->limit($start,$limit)
            ->order("id","desc")
            ->select();
        $count = db("admin_amount_detail")->where(["admin_id"=>$admin_id])->count();
        $data = [
            'list'=>$list,
            'count'=>$count
        ];
        api_return(200,"成功",$data);
    }
}